import moment from "moment";

export function formatDateTime(inputTime) {
  const date = new Date(inputTime);
  const y = date.getFullYear();
  let m: string | number = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d: string | number = date.getDate();
  d = d < 10 ? "0" + d : d;
  let h: string | number = date.getHours();
  h = h < 10 ? "0" + h : h;
  let minute: string | number = date.getMinutes();
  // let second: string | number = date.getSeconds();
  minute = minute < 10 ? "0" + minute : minute;
  // second = second < 10 ? "0" + second : second;

  return y + "/" + m + "/" + d + "," + h + ":" + minute + (+h >= 12 ? "PM" : "AM");
}

export function formatDateTime2(inputTime) {
  const date = new Date(inputTime);
  const y = date.getFullYear();
  let m: string | number = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d: string | number = date.getDate();
  d = d < 10 ? "0" + d : d;
  let h: string | number = date.getHours();
  h = h < 10 ? "0" + h : h;
  let minute: string | number = date.getMinutes();
  let second: string | number = date.getSeconds();
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;

  return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
}

export const getDateByTimeStamp = (timeStamp: number) => {
  const time2 = moment(timeStamp).utcOffset(8).format("YYYY-MM-DD HH:mm:ss");
  // 要求北京时间，时区是印度的。后端要求。
  const zone = "CST";
  return time2 + " " + zone;
};

export function formatDateByTimeZone(time: string) {
  return time.split(" ").slice(0, 2).join(" ");
}

export function getYMD(date = new Date()) {
  const year = date.getFullYear(); //获取完整的年份(4位)
  const month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  const day = date.getDate(); //获取当前日(1-31)
  return `${year}-${month}-${day}`;
}

export const isSameDay = function (t) {
  return new Date(t).toDateString() === new Date().toDateString();
};
